Shader "Unlit/cesss"
{
    Properties
    {  
        [Header(Main Properties)]
        _MainTex ("Main Texture", 2D) = "gray" {}   //"white" 
        [Toggle]_MainIsScene ("Main'UV Is Scene", Float) = 0
        [Toggle]_MainIsPolar ("Main'UV Is Polar", Float) = 0
        [Toggle]_MainIsClampX ("Main'UV Is Clamp X", Float) = 0
        [Toggle]_MainIsClampY ("Main'UV Is Clamp Y", Float) = 0
        [Toggle]_MainIsLocalX ("Main'UV Is World X", Float) = 0
        [Toggle]_MainIsLocalY ("Main'UV Is World Y", Float) = 0
        
        _MainAngle ("Main'UV Angle", Float) = 0.0
        [Enum(Vaule,0, Speed,1, Custom1.x,2, Custom1.y,3, Custom1.z,4, Custom1.w,5)]_MainUmode ("Main'UV U Offset Mode", Float) = 0
        _MainUoffset ("Main'UV U Offset", Float) = 0.0
        [Enum(Vaule,0, Speed,1, Custom1.x,2, Custom1.y,3, Custom1.z,4, Custom1.w,5)]_MainVmode ("Main'UV V Offset Mode", Float) = 0
        _MainVoffset ("Main'UV V Offset", Float) = 0.0
 
        _MainDistX ("Main'Dist X", Float) = 0
        _MainDistY ("Main'Dist Y", Float) = 0

        _MainGridX ("Main'Frame Grid X", Float) = 0
        _MainGridY ("Main'Frame Grid Y", Float) = 0
        [Toggle]_MainIsAuto ("Main'Frame Time Is Auto", Float) = 0
        _MainFrameTime ("Main'Frame Time", Float) = 0.0 
        [Space]
        [Enum(A,0, R,1, G,2, B,3, L,4)]_MainAlphaMode ("Main Use Channel=>Alpha", Float) = 0
        _MainTintColor ("Main Tint Color", Color) = (1,1,1,1)
        [Enum(None,0, Tinting,1, Multiply,2, Screen,3, Overlay,4, Normal,5)]_MainTintBlendMode ("Main Tint Blend Mode", Float) = 0.0
        _MainBrightness ("Main Brightness", Range(0.0,10.0)) = 1.0
        _MainFilterGray ("Main Filter Gray", Range(0.0,1.0)) = 0.0
        _MainFilterInvert ("Main Filter Invert", Range(0.0,1.0)) = 0.0

 
        [Header(Distort Properties)]
        [Toggle]_DistIson ("Ison", Float) = 1
        _DistTex ("Dist Texture", 2D) = "white" {}
        [Toggle]_DistIsScene ("Dist'UV Is Scene", Float) = 0
        [Toggle]_DistIsPolar ("Dist'UV Is Polar", Float) = 0
        [Toggle]_DistIsClampX ("Dist'UV Is Clamp X", Float) = 0
        [Toggle]_DistIsClampY ("Dist'UV Is Clamp Y", Float) = 0
        [Toggle]_DistIsLocalX ("Dist'UV Is World X", Float) = 0
        [Toggle]_DistIsLocalY ("Dist'UV Is World Y", Float) = 0
        
        _DistAngle ("Dist'UV Angle", Float) = 0.0
        [Enum(Vaule,0,Speed,1,Custom1.x,2,Custom1.y,3,Custom1.z,4,Custom1.w,5)]_DistUmode ("Dist'UV U Offset Mode", Float) = 0
        _DistUoffset ("Dist'UV U Offset", Float) = 0.0
        [Enum(Vaule,0,Speed,1,Custom1.x,2,Custom1.y,3,Custom1.z,4,Custom1.w,5)]_DistVmode ("Dist'UV V Offset Mode", Float) = 0
        _DistVoffset ("Dist'UV V Offset", Float) = 0.0
         

        [Header(Dissolve Properties)]
        [Toggle]_DissIson ("Ison", Float) = 1
        _DissTex ("Diss Texture", 2D) = "white" {}
        [Toggle]_DissIsScene ("Diss'UV Is Scene", Float) = 0
        [Toggle]_DissIsPolar ("Diss'UV Is Polar", Float) = 0
        [Toggle]_DissIsClampX ("Diss'UV Is Clamp X", Float) = 0
        [Toggle]_DissIsClampY ("Diss'UV Is Clamp Y", Float) = 0
        [Toggle]_DissIsLocalX ("Diss'UV Is World X", Float) = 0
        [Toggle]_DissIsLocalY ("Diss'UV Is World Y", Float) = 0
        
        _DissAngle ("Diss'UV Angle", Float) = 0.0
        [Enum(Vaule,0, Speed,1, Custom1.x,2, Custom1.y,3, Custom1.z,4, Custom1.w,5)]_DissUmode ("Diss'UV U Offset Mode", Float) = 0
        _DissUoffset ("Diss'UV U Offset", Float) = 0.0
        [Enum(Vaule,0, Speed,1, Custom1.x,2, Custom1.y,3, Custom1.z,4, Custom1.w,5)]_DissVmode ("Diss'UV V Offset Mode", Float) = 0
        _DissVoffset ("Diss'UV V Offset", Float) = 0.0
        [Enum(Vaule,0, Particle.a,1, Custom1.x,2, Custom1.y,3, Custom1.z,4, Custom1.w,5)]_DissFactorMode ("Dist Value Mode", Float) = 0
        _DissFactor ("Diss Factor", Range(1.0,0.0)) = 1.0
        _DissSoft ("soft(%)", Range(0.0, 1.0)) = 0.5
        _DissWide ("wide(%)", Range(0.0, 1.0)) = 0.5
        [HDR]_DissWideColor ("Wide Mask", Color) = (1.0, 1.0, 1.0, 1.0)

      
  
        [Header(Fresnel Properties)]
        [Toggle]_FresnelIson ("Ison", Float) = 1
        [Toggle]_FresnelFilp ("Fresnel Is Filp", Float) = 0.0
        [Toggle]_FresnelUseAlp ("Fresnel Use Alpha", Float) = 0.0
        [Toggle]_FresnelBlend ("Fresnel Use Multiply", Float) = 0.0
        // [NoScaleOffset]_FresnelTex ("Fresnel Texture", 2D) = "white" {}
        _FresnelColor ("Fresnel Color", Color) = (1,1,1,1)
        _FresnelSoft ("Fresnel Soft", Range(0.0,1.0)) = 1.0
        _FresnelWide ("Fresnel Wide", Range(0.0,1.0)) = 0.0
        _FresnelScale ("Fresnel Scale", Range(1.0,10.0)) = 1.0
        


        [Header(Ground Properties)]
        [Toggle]_GroundIson ("Ison", Float) = 1
        _GroundFactor ("Ground Factor", Float) = 0 
        _GroundSoft ("Soft(unit)", Float) = 0.5
        _GroundWide ("Edge(unit)", Float) = 0.5        
        _GroundWideSoft ("Edge Soft(unit)", Float) = 0.5
        _GroundWideColor ("Edge Color", Color) = (1,1,1,1)
    }

    CGINCLUDE   
        #define DT_USE_OPTIMIZE
        #include "DTEffectUtil.hlsl"

        sampler2D _MainTex, _DissTex,_DistTex,_MaskTex;
        // float4 _DissTex_ST, _MaskTex_ST;

        
        float GetCustomModeValue(int customMode, float customValue, float customSpeed, float customRange[6]) 
        {
            if(customMode == 0) return customValue;
            else if(customMode == 1) return customSpeed;// frac(customSpeed * _Time.y);
            int index = clamp(customMode,0,5);            
            return customRange[index];
        }

        float4 TexFilterBlend(float4 _Layer0, int _AlphaMode, float _Brightness, float4 _TintColor,int _TintBlendMode, float _FilterInvert,float _FilterGray)
        {          
            //修正色域空间的影响，最后必须还原
            float3 finalColor = saturate(dt_ToInputColor(_Layer0)).rgb;  
            //颜色            
            //修正色域空间的影响，最后必须还原
            float3 tintColor = saturate(dt_ToInputColor(_TintColor)).rgb;
            int index = clamp(_TintBlendMode,0,5);//根据下面的数组，做index限制。防止溢出报错
            float3 tintBlendModeFunc[6] =
            { 
                finalColor,                             // None
                DT_PS_COLORING(finalColor,tintColor),   //着色
                DT_PS_MULTIPLY(finalColor,tintColor),   //相乘
                DT_PS_SCREEN(finalColor,tintColor),     //滤色 
                DT_PS_OVERLAY(finalColor,tintColor),    //叠加
                DT_PS_NORMAL(finalColor,tintColor),     //正常
            };
            //计算混合
            float3 finalBlend = tintBlendModeFunc[index];
            //计算通道对混合的影响
            finalColor = DT_PS_ExpotColorBlendResultColor(finalBlend,finalColor,_TintColor.a); 
            //还原色域空间的计算结果
            finalColor =  dt_ToOutputColor3(finalColor);
            //灰度
            finalColor = DT_PS_MONO_LIGHTEN(finalColor,_FilterGray);            
            //反相
            finalColor = DT_PS_MONO_INVERT(finalColor,_FilterInvert);
            //亮度
            finalColor = finalColor * _Brightness;

            float finalAlpha = _Layer0.a;
            finalAlpha = DT_PS_MONO_CHANLLEL_TO_ALPHA(float4(_Layer0.rgb,_Layer0.a), _AlphaMode);
            return saturate(float4(finalColor,finalAlpha));
        }
    ENDCG
    SubShader
    {
       Tags
        {
            "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" 
            "PreviewType"="Plane" "CanUseSpriteAtlas"="True"
        }
        // -------------------------------------
        // Render State Commands (渲染状态命令)
        Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off 
        Fog { Mode Off }
        AlphaToMask Off
        ColorMask RGB

        // -------------------------------------
        Pass
        {
            // Name "Dissolve"
            Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
            Cull Off //Off //Back //Front //
            ZTest LEqual // Always // LEqual //            
            // ColorMask 0

            CGPROGRAM
            #pragma vertex VFXvert
            #pragma fragment VFXfrag

            
            //Properties
            //===========================================================================
            //主图属性 Main Properties
            //=====================================================================
                // UV
                    float4 _MainTex_ST; 
                    int _MainUmode; int _MainVmode; 
                    bool _MainIsScene; bool _MainIsPolar; bool _MainIsClampX; bool _MainIsClampY; bool _MainIsLocalX; bool _MainIsLocalY;            
                    float _MainDistX; float _MainDistY;
                    int _MainGridX; int _MainGridY; bool _MainIsAuto;
                    // 可动画属性 Animatable Properties
                    float _MainAngle; float _MainUoffset; float _MainVoffset; float _MainFrameTime;
                //Color
                    float _MainBrightness; int _MainAlphaMode; float4 _MainTintColor; int _MainTintBlendMode;
                //功能 
                    float _MainFilterInvert;float _MainFilterGray;
            //遮罩属性 Mask Properties
            //=====================================================================
                // float4 _MaskTex_ST, _MaskDataValue0, _MaskDataValue1;
                // float _MaskAngle;float _MaskUoffset;float _MaskVoffset;float _MaskFrameTime;
            //扰动属性 Dist Properties
            //=====================================================================
                float _DistIson;
                float4 _DistTex_ST; 
                int _DistUmode; int _DistVmode; 
                bool _DistIsScene; bool _DistIsPolar; bool _DistIsClampX; bool _DistIsClampY; bool _DistIsLocalX; bool _DistIsLocalY;
                // 可动画属性 Animatable Properties
                float _DistAngle; float _DistUoffset; float _DistVoffset;
         
            //溶解属性 Diss Properties
            //=====================================================================
                float _DissIson;
                float4 _DissTex_ST;
                int _DissUmode; int _DissVmode; 
                bool _DissIsScene; bool _DissIsPolar; bool _DissIsClampX; bool _DissIsClampY; bool _DissIsLocalX; bool _DissIsLocalY;
                // 可动画属性 Animatable Properties
                float _DissAngle; float _DissUoffset; float _DissVoffset;
                //扰动
                float _DissDistX; float _DissDistY; 
                //功能：溶解 
                int _DissFactorMode; float _DissFactor; float _DissSoft; float _DissWide; float _DissEdgeDistFactor;
                float4 _DissWideColor;
            //菲尼尔 Fresnel Properties
            //=====================================================================
                sampler2D _FresnelTex;
                float4 _FresnelColor;float _FresnelBlend ;float _FresnelUseAlp;
                float _FresnelIson ;float _FresnelSoft; float _FresnelWide; float _FresnelScale;float _FresnelFilp;
            //地面属性 Ground Properties
            //=====================================================================
                float _GroundFactor;float _GroundSoft;float _GroundWide;float _GroundIson;
                float _GroundWideSoft;float4 _GroundWideColor;
        
             
            fixed4 VFXfrag (DTVFX_v2f i) : SV_Target
            {
            //===========================================================================
                
                //基础数据
                //===================================================================== 
                    float _DTTime = _Time.y;
                    float4 BaseUV = i.uv;
                    float3 positionWS = i.positionWS;
                    float3 normalWS = i.normalWS;
                    float CustomValue[6] = {0, 0, i.Custom1.x, i.Custom1.y, i.Custom1.z, i.Custom1.w};
                    float4 vectorColor = i.vertexColor;
                 
                //扰动
                //=====================================================================
                    float2 distOffset = float2(0,0);
                    // UV
                    distOffset.x = GetCustomModeValue(_DistUmode, _DistUoffset, frac(_DistUoffset * _DTTime), CustomValue);
                    distOffset.y = GetCustomModeValue(_DistVmode, _DistVoffset, frac(_DistVoffset * _DTTime), CustomValue);
                    MatrixResult _DistUVTranRes = InitMatrixData(_DistTex_ST, _DistAngle, -distOffset, /*IsWorld*/float2(_DistIsLocalX,_DistIsLocalY));
                    float2 distuv = AppendUVMatrix( BaseUV, _DistUVTranRes, /*屏*/_DistIsScene, /*极*/_DistIsPolar, _DistIsClampX, _DistIsClampY); 
                    float _distortResult = DTtex2D(_DistTex, distuv).r * _DistIson;
                //遮罩
                //=====================================================================
                    // float _maskTex = tex2D(_MaskTex, uv + float2(0,_Time.y * -0.1)).r;
                //溶解
                //=====================================================================
                    float2 dissOffset = float2(0,0);
                    // UV
                    dissOffset.x = GetCustomModeValue(_DissUmode, _DissUoffset, frac(_DissUoffset * _DTTime), CustomValue);
                    dissOffset.y = GetCustomModeValue(_DissVmode, _DissVoffset, frac(_DissVoffset * _DTTime), CustomValue);
                    MatrixResult _DissUVTranRes = InitMatrixData(_DissTex_ST, _DissAngle, -dissOffset, /*IsWorld*/float2(_DissIsLocalX,_DissIsLocalY));
                    float2 dissuv = AppendUVMatrix( BaseUV, _DissUVTranRes, /*屏*/_DissIsScene, /*极*/_DissIsPolar, _DissIsClampX, _DissIsClampY); 
                    // 扰动
                    DistortResult _DissUVDistRes = InitDistortResult(_distortResult, float2(_DissDistX,_DissDistY));
                    AppendDistortResultUV(dissuv, _DissUVDistRes);
                    // // 序列帧
                    // float dissframe = _DissIsAuto ? (_DissFrameTime * _DTTime) % (_DissGridX * _DissGridY) : _DissFrameTime;
                    // DTFrameResult _DissUVFrameRes = InitFrameResult(_DissGridX, _DissGridY, dissframe);
                    // AppendFrameResult(dissuv, _DissUVFrameRes); 
                    float _dissValue = GetCustomModeValue(_DissFactorMode, _DissFactor, vectorColor.a, CustomValue);
                    _dissValue = 1.0 - _dissValue;
                    float4 _dissortColor= DTtex2D(_DissTex, dissuv); 
                    DissolveResult _dissResult = CalcDissolveResult_SW( _DissIson, 
                        _dissortColor.r, _dissValue, _DissSoft, _DissWide, _DissWideColor); 
                //菲尼尔
                //=====================================================================
                    FresnelResult _fnlResult = CalcFresnelResult_F010(_FresnelIson, normalWS, positionWS, _FresnelScale,/*_Soft*/ _FresnelSoft , _FresnelBlend, _FresnelUseAlp,/*_Wide*/ _FresnelWide, /*_FNLColor*/_FresnelColor
                        , /*_IsFilp*/ _FresnelFilp);
                //限定地面
                //=====================================================================
                    GroundResult _groundRes = CalcGroundResult_S(_GroundIson, positionWS.y,  _GroundFactor, _GroundSoft); 
                
                 
                //Main颜色
                //=====================================================================
                //UV变换                
                    float2 mainOffset = float2(0,0);
                    mainOffset.x = GetCustomModeValue(_MainUmode, _MainUoffset, frac(_MainUoffset * _DTTime), CustomValue);
                    mainOffset.y = GetCustomModeValue(_MainVmode, _MainVoffset, frac(_MainVoffset * _DTTime), CustomValue);
                    MatrixResult _MainUVTranRes = InitMatrixData(_MainTex_ST, _MainAngle, -mainOffset, /*IsWorld*/float2(_MainIsLocalX,_MainIsLocalY));
                    float2 mainuv = AppendUVMatrix( BaseUV, _MainUVTranRes, /*屏*/_MainIsScene, /*极*/_MainIsPolar, _MainIsClampX, _MainIsClampY); 
                    //扰动
                    DistortResult _MainUVDistRes = InitDistortResult(_distortResult, float2(_MainDistX,_MainDistY));
                    AppendDistortResultUV(mainuv, _MainUVDistRes);
                    //序列帧
                    float mainframe = _MainIsAuto ? (_MainFrameTime * _DTTime) % (_MainGridX * _MainGridY) : _MainFrameTime;
                    DTFrameResult _MainUVFrameRes = InitFrameResult(_MainGridX, _MainGridY, mainframe);
                    AppendFrameResult(mainuv, _MainUVFrameRes);
                    
                    // sample the texture 
                    float4 main = DTtex2D(_MainTex,mainuv);
                    // 颜色处理
                    // _MainTintBlendMode, _MainBrightness, _MainFilterInvert, _MainFilterGray
                    main = TexFilterBlend(main, _MainAlphaMode,_MainBrightness, _MainTintColor, _MainTintBlendMode, _MainFilterInvert, _MainFilterGray);
                    // main.rgb = max(main.r,max(main.g,main.b));
                //最终结果
                //=====================================================================
                    float3 mainColor = main.rgb;
                    float mainAlpha = main.a;
                    //
                    float3 finalColor = mainColor;
                    float finalAlpha = main.a;
                    //计算菲尼尔
                    ApplyFresnelEffect(_fnlResult,finalColor,finalAlpha);
                    //计算地面
                    ApplyGroundEffect_AC_Add(_groundRes,finalColor,finalAlpha);
                    // //计算溶解
                    ApplyDissolveEffect(1, _dissResult,/*inout*/finalColor,/*inout*/finalAlpha);
                    //最终
                    // finalColor = finalColor * finalAlpha; //如果需要预乘结果，取消这行注释
                    float4 finalCol = float4(finalColor, finalAlpha);
                    return finalCol;
            //===================================================================== 
            }
            ENDCG
        }
    }
}
